/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OpenKnowledBasScreen.java
 *
 * Created on 07/08/2009, 00:47:30
 */
package gui;

import base.KnowledgeBaseManager;
import exceptions.UnBalancedExpressionException;
import exceptions.UnDecidibleValueException;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import listeners.ClickToPdfListener;
import logicaDePredicados.LogicOperatorFactory;
import logicaDePredicados.LogicPredicate;
import logicaDePredicados.PredicateConstant;
import logicaDePredicados.PredicateLogic;
import logicaDePredicados.PredicateVariable;
import logicaDePredicados.QuantifierStrategy;
import logicaDePredicados.Sentence;
import logicaDePredicados.SimpleStrategy;
import logicaDePredicados.SkolemFunction;

/**
 *
 * @author Francisco Gindre
 */
public class OpenKnowledgeBaseScreen extends javax.swing.JFrame {

    private File selectedFile;
    private List<PredicateLogic> resultList;
    private ArrayList<PredicateVariable<String>> variableList;
    private Sentence sentence;
    /** Creates new form OpenKnowledBasScreen */
    public OpenKnowledgeBaseScreen(File selectedFile) {
        this.setLocationRelativeTo(this.getParent());
        this.selectedFile = selectedFile;
        this.setVisible(true);
        sentence = new Sentence(new SimpleStrategy());
        try {
            resultList = KnowledgeBaseManager.ObtainEntireKnowledgeBase(selectedFile);
        } catch (Exception ex) {
            ErrorFrame error = new ErrorFrame("Database Error: Could not open DB file");
            error.setVisible(true);
            this.dispose();
        }
        variableList = new ArrayList<PredicateVariable<String>>();
        variableList.add(new PredicateVariable<String>("x", "x"));
        variableList.add(new PredicateVariable<String>("y", "y"));
        variableList.add(new PredicateVariable<String>("z", "z"));


        initComponents();
        valueList.setModel(new EmptyListModel());
        valueList.setSize(predicateList.getHeight(), predicateList.getWidth());
        valueList.setMinimumSize(new Dimension(predicateList.getHeight(), predicateList.getWidth()));
        valueList.setMaximumSize(new Dimension(predicateList.getHeight(), predicateList.getWidth()));



    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        evaluateButon = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        sentenceTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        predicateList = new javax.swing.JList();
        forAllButton = new javax.swing.JButton();
        existsButton = new javax.swing.JButton();
        andButton = new javax.swing.JButton();
        orButton = new javax.swing.JButton();
        notButton = new javax.swing.JButton();
        thenButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        operationLabel = new javax.swing.JLabel();
        addConstantButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        variableComboBox = new javax.swing.JComboBox();
        newButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        addToSentenceButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        valueList = new javax.swing.JList();
        addFunctionButton = new javax.swing.JButton();
        leftBracketButton = new javax.swing.JButton();
        rightBracketButton = new javax.swing.JButton();
        formatCheckBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PredicateLogic Calculator 1e-15!");
        setMinimumSize(new java.awt.Dimension(682, 411));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Base File:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(selectedFile.getAbsolutePath());
        jLabel2.setName("jLabel2"); // NOI18N

        evaluateButon.setText("Evaluate!");
        evaluateButon.setName("evaluateButon"); // NOI18N
        evaluateButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evaluateButonActionPerformed(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        sentenceTextArea.setColumns(20);
        sentenceTextArea.setEditable(false);
        sentenceTextArea.setLineWrap(true);
        sentenceTextArea.setRows(5);
        sentenceTextArea.setName("sentenceTextArea"); // NOI18N
        jScrollPane2.setViewportView(sentenceTextArea);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/icons/aboutLogo.png"))); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        predicateList.setModel(new PredicateLogicListModel (resultList));
        predicateList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        predicateList.setToolTipText("Double-Click to see values");
        predicateList.setName("predicateList"); // NOI18N
        predicateList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                predicateListMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(predicateList);

        forAllButton.setText("ForAll");
        forAllButton.setName("forAllButton"); // NOI18N
        forAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forAllButtonActionPerformed(evt);
            }
        });

        existsButton.setText("Exists");
        existsButton.setName("existsButton"); // NOI18N
        existsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                existsButtonActionPerformed(evt);
            }
        });

        andButton.setText("and");
        andButton.setName("andButton"); // NOI18N
        andButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                andButtonActionPerformed(evt);
            }
        });

        orButton.setText("or");
        orButton.setName("orButton"); // NOI18N
        orButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orButtonActionPerformed(evt);
            }
        });

        notButton.setText("not");
        notButton.setName("notButton"); // NOI18N
        notButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notButtonActionPerformed(evt);
            }
        });

        thenButton.setText("then");
        thenButton.setName("thenButton"); // NOI18N
        thenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thenButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear!");
        clearButton.setName("clearButton"); // NOI18N
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        operationLabel.setText("Operations");
        operationLabel.setName("operationLabel"); // NOI18N

        addConstantButton.setText("add constant");
        addConstantButton.setName("addConstantButton"); // NOI18N
        addConstantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addConstantButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Variables");
        jLabel4.setName("jLabel4"); // NOI18N

        variableComboBox.setModel(new javax.swing.DefaultComboBoxModel(variableList.toArray()));
        variableComboBox.setName("variableComboBox"); // NOI18N

        newButton.setText("new");
        newButton.setName("newButton"); // NOI18N
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        removeButton.setText("remove");
        removeButton.setName("removeButton"); // NOI18N
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("Values");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Predicates & Skolem");
        jLabel6.setName("jLabel6"); // NOI18N

        addToSentenceButton.setText("add to Sentence");
        addToSentenceButton.setName("addToSentenceButton"); // NOI18N
        addToSentenceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToSentenceButtonActionPerformed(evt);
            }
        });

        helpButton.setText("Help!!");
        helpButton.setToolTipText("mommy?");
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        valueList.setModel(new EmptyListModel());
        valueList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        valueList.setToolTipText("Select item and then click add constant to add it to sentence");
        valueList.setName("valueList"); // NOI18N
        valueList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                valueListMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(valueList);
        valueList.getAccessibleContext().setAccessibleParent(jScrollPane3);

        addFunctionButton.setText("add function");
        addFunctionButton.setToolTipText("add selected function to sentence");
        addFunctionButton.setName("addFunctionButton"); // NOI18N
        addFunctionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addFunctionButtonActionPerformed(evt);
            }
        });

        leftBracketButton.setText("(");
        leftBracketButton.setName("leftBracketButton"); // NOI18N
        leftBracketButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftBracketButtonActionPerformed(evt);
            }
        });

        rightBracketButton.setText(")");
        rightBracketButton.setName("rightBracketButton"); // NOI18N
        rightBracketButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightBracketButtonActionPerformed(evt);
            }
        });

        formatCheckBox.setText("show  formatting");
        formatCheckBox.setName("formatCheckBox"); // NOI18N
        formatCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formatCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(leftBracketButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(rightBracketButton))
                                .addComponent(addFunctionButton, javax.swing.GroupLayout.Alignment.LEADING)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(forAllButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(existsButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(16, 16, 16)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(notButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(thenButton))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(andButton)
                                                .addGap(18, 18, 18)
                                                .addComponent(orButton, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)))
                                        .addGap(18, 18, 18)
                                        .addComponent(variableComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(operationLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(addToSentenceButton, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel4)
                                            .addComponent(newButton))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(removeButton))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(formatCheckBox)
                                        .addGap(41, 41, 41)
                                        .addComponent(helpButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(clearButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(deleteButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(evaluateButon))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(68, 68, 68)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(addConstantButton)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel3)
                .addGap(27, 27, 27)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(evaluateButon)
                            .addComponent(deleteButton)
                            .addComponent(clearButton)
                            .addComponent(helpButton)
                            .addComponent(formatCheckBox))
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(operationLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(forAllButton)
                            .addComponent(andButton)
                            .addComponent(orButton)
                            .addComponent(variableComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(newButton)
                            .addComponent(removeButton)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(addFunctionButton))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(existsButton)
                    .addComponent(notButton)
                    .addComponent(thenButton)
                    .addComponent(addToSentenceButton)
                    .addComponent(addConstantButton)
                    .addComponent(leftBracketButton)
                    .addComponent(rightBracketButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void predicateListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_predicateListMouseClicked
        PredicateLogic selected = (PredicateLogic) predicateList.getSelectedValue();
        if (selected.getClass().equals(SkolemFunction.class)) {
            valueList.setModel(new ListModel(((SkolemFunction<String, String>) selected).getKeyList()));
        } else {
            if (selected.getClass().equals(LogicPredicate.class)) {
                valueList.setModel(new ListModel(((LogicPredicate<String>) selected).getKeyList()));
            } else {
                new ErrorFrame("Illegal element in PredicateLogic list").setVisible(true);
                this.dispose();
            }
        }

    }//GEN-LAST:event_predicateListMouseClicked

    private void refreshDisplay(){
        sentenceTextArea.setText(null);
        if (formatCheckBox.isSelected())
            sentenceTextArea.setText(sentence.toStringWithValues());
        else
            sentenceTextArea.setText(sentence.rawFormat());

        

    }
    public void addAndRefreshComboBox (PredicateVariable<String> var){
        variableList.add(var);
        variableComboBox.setModel(new javax.swing.DefaultComboBoxModel(variableList.toArray()));
    }
    private void valueListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_valueListMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_valueListMouseClicked

    private void forAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forAllButtonActionPerformed
        /**
         * verify that the strategy is correct
         */
        if (sentence.getParser().getClass().equals(SimpleStrategy.class)){
            sentence.setParser(new QuantifierStrategy());

        }
        sentence.add(LogicOperatorFactory.createEssentialQuantifier());
        refreshDisplay();

    }//GEN-LAST:event_forAllButtonActionPerformed

    private void andButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_andButtonActionPerformed
        sentence.add(LogicOperatorFactory.createAND());
        refreshDisplay();
    }//GEN-LAST:event_andButtonActionPerformed

    private void orButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orButtonActionPerformed
        sentence.add(LogicOperatorFactory.createOR());
        refreshDisplay();
    }//GEN-LAST:event_orButtonActionPerformed

    private void existsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_existsButtonActionPerformed
        /**
         * verify that the strategy is correct
         */
        if (sentence.getParser().getClass().equals(SimpleStrategy.class)){
            sentence.setParser(new QuantifierStrategy());

        }
        sentence.add(LogicOperatorFactory.createExistentialQuantifier());
        refreshDisplay();
    }//GEN-LAST:event_existsButtonActionPerformed

    private void notButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notButtonActionPerformed
        sentence.add(LogicOperatorFactory.createNOT());
        refreshDisplay();
    }//GEN-LAST:event_notButtonActionPerformed

    private void leftBracketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leftBracketButtonActionPerformed
        sentence.add(LogicOperatorFactory.createLeftBracket());
        refreshDisplay();
    }//GEN-LAST:event_leftBracketButtonActionPerformed

    private void rightBracketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightBracketButtonActionPerformed
       sentence.add(LogicOperatorFactory.createRightBraket());
       refreshDisplay();
    }//GEN-LAST:event_rightBracketButtonActionPerformed

    private void thenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thenButtonActionPerformed
        sentence.add(LogicOperatorFactory.createTHEN());
        refreshDisplay();
    }//GEN-LAST:event_thenButtonActionPerformed

    private void addFunctionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFunctionButtonActionPerformed
        if (predicateList.getSelectedValue()!=null)
            sentence.add((PredicateLogic)predicateList.getSelectedValue());
        else
            new ErrorFrame ("No function item selected").setVisible(true);
        refreshDisplay();
    }//GEN-LAST:event_addFunctionButtonActionPerformed

    private void addToSentenceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToSentenceButtonActionPerformed
        sentence.add((PredicateLogic) variableComboBox.getSelectedItem());
        refreshDisplay();

    }//GEN-LAST:event_addToSentenceButtonActionPerformed

    private void addConstantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addConstantButtonActionPerformed
        if (valueList.getSelectedValue()!=null){

        PredicateConstant<String> constant = new PredicateConstant<String>((String)valueList.getSelectedValue(),(String)valueList.getSelectedValue());

        sentence.add(constant);
        refreshDisplay();
        } else {
            new ErrorFrame("no value selected").setVisible(true);
        }



    }//GEN-LAST:event_addConstantButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
       NewVariableFrame newVarFrame = new NewVariableFrame( this);
       newVarFrame.setVisible(true);

    }//GEN-LAST:event_newButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        variableList.remove((PredicateVariable<String>)variableComboBox.getSelectedItem());
        variableComboBox.setModel(new javax.swing.DefaultComboBoxModel(variableList.toArray()));

    }//GEN-LAST:event_removeButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        sentence.clear();
        sentence.setParser(new SimpleStrategy());
        sentenceTextArea.setBackground(Color.white);
        System.gc();
        refreshDisplay();
    }//GEN-LAST:event_clearButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (sentence.size()>0)
        sentence.remove(sentence.size()-1);
        if (sentence.size()==0){
            sentence.setParser(new SimpleStrategy());
            System.gc();
        }
        refreshDisplay();

    }//GEN-LAST:event_deleteButtonActionPerformed

    private void evaluateButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_evaluateButonActionPerformed
        try {
            if (sentence.parse()) {
                sentenceTextArea.setBackground(new Color(153, 255, 153));
            }else {
                sentenceTextArea.setBackground(new Color(255, 153, 153));
            }
            if(sentence.getParser().getClass().equals(QuantifierStrategy.class)){
                sentence.setParser(null);
                sentence.setParser(new QuantifierStrategy());
            }
            else {
                sentence.setParser(null);
                sentence.setParser(new SimpleStrategy());
            }

        } catch (UnBalancedExpressionException ex) {
            new ErrorFrame(ex.getMessage()).setVisible(true);

        } catch (UnDecidibleValueException ex) {
            new ErrorFrame(ex.getMessage()).setVisible(true);
            sentenceTextArea.setBackground(new Color (153,153,255));
        } catch (IllegalAccessError ex) {
            new ErrorFrame("Parser Error", ex.getMessage());
        }
    }//GEN-LAST:event_evaluateButonActionPerformed

    private void formatCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formatCheckBoxActionPerformed
        refreshDisplay();
    }//GEN-LAST:event_formatCheckBoxActionPerformed

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        new ClickToPdfListener(System.getProperty("user.dir") + "./Manualdelusuario.pdf").actionPerformed(evt);
    }//GEN-LAST:event_helpButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addConstantButton;
    private javax.swing.JButton addFunctionButton;
    private javax.swing.JButton addToSentenceButton;
    private javax.swing.JButton andButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton evaluateButon;
    private javax.swing.JButton existsButton;
    private javax.swing.JButton forAllButton;
    private javax.swing.JCheckBox formatCheckBox;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton leftBracketButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton notButton;
    private javax.swing.JLabel operationLabel;
    private javax.swing.JButton orButton;
    private javax.swing.JList predicateList;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton rightBracketButton;
    private javax.swing.JTextArea sentenceTextArea;
    private javax.swing.JButton thenButton;
    private javax.swing.JList valueList;
    private javax.swing.JComboBox variableComboBox;
    // End of variables declaration//GEN-END:variables
}
